<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Java SE-修饰符 |  Yolin
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.png" />
  
  
<link rel="stylesheet" href="/css/style.css">

  
<script src="/js/pace.min.js"></script>


  

  

<link rel="alternate" href="/atom.xml" title="Yolin" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-Java-SE-修饰符" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Java SE-修饰符
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/07/16/Java-SE-%E4%BF%AE%E9%A5%B0%E7%AC%A6/" class="article-date">
  <time datetime="2020-07-16T12:50:02.000Z" itemprop="datePublished">2020-07-16</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/Java/">Java</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.2k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">3分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h2 id="一、访问控制修饰符"><a href="#一、访问控制修饰符" class="headerlink" title="一、访问控制修饰符"></a>一、访问控制修饰符</h2><p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-07-15_10-34-00.jpg" alt=""></p>
<ol>
<li>一个源文件可以有多个类，但只能有一个public类如果有其他类只能以默认修饰。</li>
<li>声明属性和方法尽可能私有，这样才能做到尽可能的封装</li>
</ol>
<h2 id="二、常用的转义字符"><a href="#二、常用的转义字符" class="headerlink" title="二、常用的转义字符"></a>二、常用的转义字符</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"\b"</span>  (退格)</span><br><span class="line"><span class="string">"\f"</span>  (换页)</span><br><span class="line"><span class="string">"\n"</span>  (换行)</span><br><span class="line"><span class="string">"\r"</span>  (回车)</span><br><span class="line"><span class="string">"\t"</span>  (水平制表符(到下一个tab位置))</span><br><span class="line"><span class="string">"\' "</span>  (单引号)</span><br><span class="line"><span class="string">"\" "</span>  (双引号)</span><br><span class="line"><span class="string">"\\"</span>  (反斜杠)</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h2 id="三、状态修饰符-final-static"><a href="#三、状态修饰符-final-static" class="headerlink" title="三、状态修饰符(final\static)"></a>三、状态修饰符(final\static)</h2><h3 id="1、final关键字"><a href="#1、final关键字" class="headerlink" title="1、final关键字"></a>1、final关键字</h3><p>意思为最终的，可以修饰成员变量，成员方法和类</p>
<h4 id="final修饰的类不能再被继承"><a href="#final修饰的类不能再被继承" class="headerlink" title="final修饰的类不能再被继承"></a>final修饰的类不能再被继承</h4><ul>
<li>Java的String、Math、Integer、Long等就是final类，不能被继承</li>
<li>Spring，Struts 2，这些看框架使用了“动态继承代理”技术，使用final的类会影响该技术的实现</li>
</ul>
<h4 id="final修饰的方法是最终方法，不能再被覆盖"><a href="#final修饰的方法是最终方法，不能再被覆盖" class="headerlink" title="final修饰的方法是最终方法，不能再被覆盖"></a>final修饰的方法是最终方法，不能再被覆盖</h4><p>实际开发中，原则上不允许使用final方法！</p>
<h4 id="final修饰的变量表明该变量是常量，不能再次被赋值"><a href="#final修饰的变量表明该变量是常量，不能再次被赋值" class="headerlink" title="final修饰的变量表明该变量是常量，不能再次被赋值"></a>final修饰的变量表明该变量是常量，不能再次被赋值</h4><h5 id="修饰局部变量"><a href="#修饰局部变量" class="headerlink" title="修饰局部变量"></a>修饰局部变量</h5><ul>
<li>变量是基本类型：final修饰指的是基本类型的<strong>数据值</strong>不能发生改变</li>
<li>变量是引用类型：final修饰指的是引用类型的<strong>地址值</strong>不能发生改变，但是地址里面的内容可以发生改变</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-07-16_18-10-43.jpg" alt=""></p>
<h3 id="2、static关键字"><a href="#2、static关键字" class="headerlink" title="2、static关键字"></a>2、static关键字</h3><p>static是静态的意思，可以修饰<strong>成员方法，成员变量，代码块</strong></p>
<h4 id="static修饰的特点"><a href="#static修饰的特点" class="headerlink" title="static修饰的特点"></a>static修饰的特点</h4><p><strong>被类的所有对象共享</strong>（这也是我们判断是否使用静态关键字的条件）</p>
<p>使用static声明的方法，属于类的方法，一般用来表示工具方法，会在类加载后自己调用，不需要创建任何类的实例对象。</p>
<p>通俗理解为：</p>
<ol>
<li>如果不加static修饰的成员是对象成员，归每个对象所有</li>
<li>加static修饰的成员是类成员，可由一个类直接调用，为所有对象共有的。</li>
</ol>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-07-16_18-45-10.jpg" alt=""></p>
<h4 id="static访问特点"><a href="#static访问特点" class="headerlink" title="static访问特点"></a>static访问特点</h4><p><strong>非静态的成员方法</strong></p>
<ul>
<li>能访问静态的成员变量和成员方法</li>
<li>能访问非静态的成员变量和非静态的成员方法</li>
</ul>
<p><strong>静态的成员方法</strong></p>
<ul>
<li>只能访问静态的成员变量和成员方法</li>
</ul>
<h4 id="静态代码块"><a href="#静态代码块" class="headerlink" title="静态代码块"></a>静态代码块</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span>&#123;</span><br><span class="line">	System.out.println(<span class="string">"类加载了"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>静态代码块在类加载期间执行，由于类只加载一次，所以静态代码块只执行一次，用在加载配置文件等</p>
<h2 id="四、abstract修饰符"><a href="#四、abstract修饰符" class="headerlink" title="四、abstract修饰符"></a>四、abstract修饰符</h2><p>abstract表示抽象的，可以修饰类和方法。分别叫抽象方法和抽象类。</p>
<p>人们在认识事物的时候，会把具有相同特征和行为的事物归为一个抽象类，例如动物就是抽象的概念，当得到动物实例时，总是某个具体物种的实例。</p>
<p>抽象类不能实例化，不能生成抽象类的对象，但能定义一个引用。</p>
<h4 id="abstract修饰类"><a href="#abstract修饰类" class="headerlink" title="abstract修饰类"></a>abstract修饰类</h4><p>会使这个类成为一个<strong>抽象类</strong>，这个类将不能生成对象实例，但可以作为对象变量声明的类型。</p>
<p>抽象类就相当于一个半成品，需要子类继承并覆盖其中的方法，这时子类才又创建实例的能力，如果子类没有实现父类的抽象方法，如果子类没有实现父类的抽象方法，那么子类也要为抽象类。</p>
<h4 id="abstract修饰方法"><a href="#abstract修饰方法" class="headerlink" title="abstract修饰方法"></a>abstract修饰方法</h4><p>会使这个方法称为<strong>抽象方法</strong>，也就是只有声明没有实现，实现部分以”;”代替，需要子类继承实现。</p>
<p>抽象方法代表了某种标准，定义标准，定义功能，在子类中实现功能(子类继承了父类并需要给出从父类继承的抽象方法的实现)</p>
<p><strong>方法一时间想不到怎么被实现，或有意要子类去实现而定义某种标椎，这个方法可以被定义为抽象</strong>。</p>
<h4 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h4><ul>
<li><strong>有抽象方法的类一定是抽象类。但是抽象类中不一定都是抽象方法。</strong></li>
<li><strong>当一个非抽象类继承自某个抽象类时，必须实现所继承抽象类的所有抽象方法</strong>（即抽象类的第一个非抽象子类必须要实现其父类所有抽象方法）</li>
<li>abstract和final不能同时使用，这两个关键字有着相反的意思，abstract修饰方法和类，就是想让别人重写或是继承的，而final是阻止的。</li>
<li>private和abstract也不能同时修饰方法<br>使用抽象类的思想实现猫狗案例</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-07-17_13-00-28.jpg" alt=""></p>

      
      <!-- reward -->
      
    </div>
      <!-- copyright -->
      
    <footer class="article-footer">
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/abstract/" rel="tag">abstract</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/final/" rel="tag">final</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/static/" rel="tag">static</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/07/20/Java-SE-%E6%8E%A5%E5%8F%A3/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Java SE-接口
          
        </div>
      </a>
    
    
      <a href="/2020/07/15/Java-SE-%E5%A4%9A%E6%80%81/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Java SE-多态</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: '',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020-2021
        张金
      </li>
      <li>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
        <img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/beian.png">
        <a href="https://beian.miit.gov.cn"  style="color:#f72b07" target="_blank">陕ICP备20002449号</a>
      </li>

    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Yolin"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/playlist">歌单</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/video">视频</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友情链接</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<script src="/js/share.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['肠断月明红豆蔻,','月似当时,','人似当时否？'],
    startDelay: 1500,
    typeSpeed: 200,
    loop: true,
    backSpeed: 40,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: true
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>



<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>